package com.yswee.framework.widget.layoutview;

import com.yswee.framework.R;
import com.yswee.framework.activity.IActivityContext;
import com.yswee.framework.manager.ActivityManager;
import com.yswee.framework.utils.UnitUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class WrapViewGroup extends ViewGroup {

	protected int mMarginHorizontal;
	protected int mMarginVertical;
	protected Context mContext;

	public WrapViewGroup(Context context) {
		this(context, null);
	}

	public WrapViewGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		initializeAttr(attrs);
	}

	public WrapViewGroup(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		initializeAttr(attrs);
	}

	protected void initializeAttr(AttributeSet attrs) {
		mMarginHorizontal = UnitUtils.dip2pix(getContext(), 10);
		mMarginVertical = UnitUtils.dip2pix(getContext(), 10);

		TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.WrapViewGroup);
		mMarginHorizontal = a.getInt(R.styleable.WrapViewGroup_horizontal_margin, mMarginHorizontal);
		mMarginVertical = a.getInt(R.styleable.WrapViewGroup_vertical_margin, mMarginVertical);
		a.recycle();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		// 获得它的父容器为它设置的测量模式和大小
		int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
		int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
		int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
		int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
		// 如果是warp_content情况下，记录宽和高
		int width = 0;
		int height = 0;
		int lineWidth = 0;
		int lineHeight = 0;

		int cCount = getChildCount();

		// 遍历每个子元素
		for (int i = 0; i < cCount; i++) {
			View child = getChildAt(i);
			if (child.getVisibility() == View.GONE) {
				continue;
			}
			// 测量每一个child的宽和高
			measureChild(child, widthMeasureSpec, heightMeasureSpec);

			// 当前子空间实际占据的宽度
			int childWidth = child.getMeasuredWidth() + mMarginHorizontal;
			// 当前子空间实际占据的高度
			int childHeight = child.getMeasuredHeight();
			/**
			 * 如果加入当前child，则超出最大宽度，则的到目前最大宽度给width，类加height 然后开启新行
			 */
			if (lineWidth + childWidth > sizeWidth) {
				width = Math.max(width, lineWidth);// 取最大的
				lineWidth = childWidth; // 重新开启新行，开始记录
				// 叠加当前高度，
				height += lineHeight + mMarginVertical;
				// 开启记录下一行的高度
				lineHeight = childHeight;
			} else
			// 否则累加值lineWidth,lineHeight取最大高度
			{
				lineWidth += childWidth;
				lineHeight = Math.max(lineHeight, childHeight);
			}
		}
		width = Math.max(width, lineWidth);
		height += lineHeight;
		setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : width,
				(modeHeight == MeasureSpec.EXACTLY) ? sizeHeight : height);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		final int count = getChildCount();
		int row = 0;
		int lengthX = 0;
		int lengthY = 0;
		for (int i = 0; i < count; i++) {
			final View child = this.getChildAt(i);
			if (child.getVisibility() == View.GONE) {
				continue;
			}
			int width = child.getMeasuredWidth() + mMarginHorizontal;
			int height = child.getMeasuredHeight();
			lengthX += width;
			lengthY = row * (height + mMarginVertical) + height;
			// 换行
			if (lengthX > r - l) {
				lengthX = width;
				row++;
				lengthY = row * (height + mMarginVertical) + height;
			}
			child.layout(lengthX - width, lengthY - height, lengthX - mMarginHorizontal, lengthY);
		}
	}
	
	protected void runOnUiThread(Runnable action) {
		Activity activity = null;
		if (mContext instanceof Activity) {
			activity = (Activity) mContext;
		} else {
			IActivityContext context = ActivityManager.get().currentActivity();
			if (context != null) {
				activity = (Activity) context;
			}
		}
		if (activity != null) {
			activity.runOnUiThread(action);
		}
	}
	
	protected void startActivity(Class<?> cls) {
		Intent intent = new Intent(mContext, cls);
		startActivity(intent);
	}
	
	protected void startActivityForResult(Class<?> cls, int requestCode) {
		Intent intent = new Intent(mContext, cls);
		startActivityForResult(intent, requestCode);
	}
	
	public void startActivity(Intent intent) {
		mContext.startActivity(intent);
    }
	
	public void startActivityForResult(Intent intent, int requestCode) {
		Activity activity = (Activity) mContext;
		activity.startActivityForResult(intent, requestCode);
    }
}